/**
 * AreaController.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年2月3日
 */
package com.pshare.artemis.basic.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pshare.base.bean.SeaxResponse;
import com.pshare.framework.controller.WebController;
import com.pshare.framework.page.PageInfo;
import com.pshare.artemis.basic.bean.City;
import com.pshare.artemis.basic.bean.District;
import com.pshare.artemis.basic.bean.Landmark;
import com.pshare.artemis.basic.bean.Province;
import com.pshare.artemis.basic.service.AreaService;

/**
 * 地区接口
 */
@Controller
public class AreaController extends WebController {
	
	private static final Logger logger = Logger.getLogger(AreaController.class);

	@Autowired
	private AreaService areaService;

	@RequestMapping(value = "/area", method = RequestMethod.GET)
	@ResponseBody
	public SeaxResponse<Map<String, Object>> getArea() {
		Map<String, Object> data = new HashMap<String, Object>();
		
		try {
			List<Province> ps = areaService.getAllProvinces();
			List<City> cs = areaService.getAllCities();
			List<District> rs = areaService.getAllDistricts();
			
			data.put("province", ps);
			data.put("city", cs);
			data.put("district", rs);
		} catch (Exception e) {
			logger.error(e);
			return failed();
		}
		return succeed(data);
	}
	
	/**
	 * 根据城市名模糊查找城市
	 * @param query 城市名
	 * @return SeaxResponse<List<City>>
	 */
	@RequestMapping(value = "/area/province/{provinceId}/city", method = RequestMethod.GET)
	@ResponseBody
	public SeaxResponse<List<City>> getCitiesByProvinceId(@PathVariable Integer provinceId) {
		List<City> list = areaService.getCitiesByProvinceId(provinceId);
		return succeed(list);
	}
	
	/**
	 * 根据城市名模糊查找城市
	 * @param query 城市名
	 * @return SeaxResponse<PageInfo<City>>
	 */
	@RequestMapping(value = "/area/city/query", method = RequestMethod.GET)
	@ResponseBody
	public SeaxResponse<PageInfo<City>> queryCity(String query) {
		PageInfo<City> pageInfo = areaService.queryCity(query);
		return succeed(pageInfo);
	}
	
	/**
	 * 根据城市ID读取城市名称
	 * @param cityId
	 * @return SeaxResponse<String>
	 */
	@RequestMapping(value = "/area/city/name/{cityId}", method = RequestMethod.GET)
	@ResponseBody
	public SeaxResponse<String> getCityName(@PathVariable Integer cityId) {
		String cityName = areaService.getCityName(cityId);
		return succeed(cityName);
	}
	
	@RequestMapping(value = "/area/landmarks/{districtId}", method = RequestMethod.GET)
	@ResponseBody
	public SeaxResponse<Map<String, Object>> getLandmarks(@PathVariable Integer districtId) {
		List<Landmark> landamarks = areaService.getLandMarks(districtId);
		return succeed(landamarks);
	}
	
	
}
